package kroketwebspring.domain;

public class Product {
	private int id;
	private String name;
	private String price;
	private String totalPrice;
	private float fPrice;
	private int stock;
	private int amount;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPrice() {
		return price;
	}
	
	public String getTotalPrice() {
		if (totalPrice.equals("") || totalPrice == null) {
			calcTotalPrice();
		}
		return totalPrice;
	}

	public void calcTotalPrice() {
		Float totalPrice = amount * fPrice;
		float p = (float)Math.pow(10,2);
		totalPrice = totalPrice * p;
		float tmp = Math.round(totalPrice);
		totalPrice = (float) tmp / p;
		
		String sPrice = totalPrice.toString();
		sPrice = sPrice.replaceAll("\\.", "\\,");

		String values[] = sPrice.split(",");
		if (values[1].length() == 1) {
			values[1] += "0";
		}

		sPrice = values[0] + "," + values[1];
		
		this.totalPrice = sPrice;
	}

	public void setPrice(Float price) {
		fPrice = price;
		// Convert the float price to a nicely formatted currency string
		String sPrice = price.toString();
		sPrice = sPrice.replaceAll("\\.", "\\,");

		String values[] = sPrice.split(",");
		if (values[1].length() == 1) {
			values[1] += "0";
		}

		sPrice = values[0] + "," + values[1];
		this.price = sPrice;
	}

	public int getStock() {
		return stock;
	}

	public void setStock(int stock) {
		this.stock = stock;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		// calc total price
		calcTotalPrice();
		this.amount = amount;
	}
}
